﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.Common
{
    public static class GridHelper
    {
        /// <summary>
        /// 获取枚举类型的表格显示值转换
        /// </summary>
        /// <param name="functionName">Javascript方法名(建议命名为GConvert枚举类名)</param>
        /// <returns></returns>
        public static string GridFormatterFunc<T>(string functionName)
        {
            return EnumMvcHelper.GridFormatterFunc<T>(functionName);
        }

        /// <summary>
        /// 获取实体类型的表格显示值转换
        /// </summary>
        /// <param name="functionName">Javascript方法名(建议命名为GConvert实体类名)</param>
        /// <returns></returns>
        //public static string GridFormatterFunc<T>(List<T> source,string indexColumn,string valueColumn,string functionName)
        //{
        //    StringBuilder retFunc = new StringBuilder("var source").Append(typeof(T).Name).Append("=").Append(JsonConvert.SerializeObject(source)).Append(";\n")
        //        .Append("function (val) {\n");
        //    retFunc.Append("$.each(function(){ \n")
        //           .Append("if(val==this.").Append(indexColumn).Append("){ \n")
        //           .Append("return this.").Append(valueColumn).Append("\n}\n}\n return '';}");
        //    return retFunc.ToString();
        //}

        /// <summary>
        /// 获取实体类型的表格显示值转换 zph
        /// </summary>
        /// <param name="functionName">Javascript方法名(建议命名为GConvert实体类名)</param>
        /// <returns></returns>
        public static string GridFormatterFunc<T>(List<T> source, string indexColumn, string valueColumn, string functionName)
        { 
            string retFunc = string.Format(@"
            function {0}(val) 
            {4}
                var data= {3};
                var txt = '';
                $.each(data, function(){4}
                    if (val == this.{1})
                    {4}
                        txt = this.{2};
                        return false;
                    {5}
                {5}); 
                return txt;
            {5}", functionName, indexColumn, valueColumn, JsonConvert.SerializeObject(source), "{","}");
            return retFunc ;
        }

    }
}
